package com.hongxun.budget.ui.activity.tk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.BDeviceEntityDao;
import com.hongxun.budget.greendao.BPointConfigEntityDao;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.ui.adapter.SpinnerOffBgAdapter;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.T;
import com.hongxun.greenentity.tk.BDeviceEntity;
import com.hongxun.greenentity.tk.BPointConfigEntity;
import com.hongxun.interfaces.ResponseListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 添加设备
 */
public class AddDeviceActivity extends BaseActivity {


    @BindView(R.id.center_spinner)
    Spinner centerSpinner;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.search_info)
    EditText searchInfo;
    @BindView(R.id.list2)
    RecyclerView list2;
    DaoSession session;

    List<String> leftDatas = new ArrayList<>();
    SuperAdapter<String> leftAdapter;
    int left = 0;//记录left的选择
    List<BDeviceEntity> deviceEntities = new ArrayList<>();
    SuperAdapter<BDeviceEntity> rightAdapter;
    String label, label2, search, intentLabel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_device;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        intentLabel = getIntent().getStringExtra("label");
        session = GreenDaoManager.getInstance().getmDaoSession();
        final BPointConfigEntity b = session.getBPointConfigEntityDao().queryBuilder()
                .where(BPointConfigEntityDao.Properties.PCLive.eq(intentLabel), BPointConfigEntityDao.Properties.PCProcess.eq("0")).unique();
        List<String> labels = new ArrayList<>();
        if (b != null)
            labels = Arrays.asList(b.getPCLabel().split(","));
        else
            T.showLong(context, "该点位缺少配置，请联系管理员");
        centerSpinner.setAdapter(new SpinnerOffBgAdapter<String>(context, labels, centerSpinner)
                .setListener(new ResponseListener() {
                    @Override
                    public void response(Object... data) {
                        searchInfo.setText("");
                        left = 0;
                        label = (String) data[1];
                        leftDatas.clear();
                        deviceEntities.clear();
                        setLeftList();
                    }
                }));
        list.setLayoutManager(RecyclerViewUtils.getLinearLayoutManagerForVertical(context));
        list.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        list.setAdapter(leftAdapter = new SuperAdapter<String>(R.layout.content_adddevice_left) {
            @Override
            public void bindView(ViewHolder viewHolder, final String s, final int i) {
                TextView t = viewHolder.getView(R.id.label);
                t.setText(s);
                if (i == left) {
                    t.setBackgroundResource(R.drawable.view_bottom_textpre);
                } else {
                    t.setBackgroundResource(R.color.white);
                }
                t.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        searchInfo.setText("");
                        left = i;
                        label2 = s;
                        leftAdapter.notifyDataSetChanged();
                        setRightList();
                    }
                });
            }
        });
        leftAdapter.setData(leftDatas);
        leftAdapter.setEmptyDataView(R.layout.view_empty);
        list2.setLayoutManager(RecyclerViewUtils.getLinearLayoutManagerForVertical(context));
        list2.setAdapter(rightAdapter = new SuperAdapter<BDeviceEntity>(R.layout.content_add_device) {
            @Override
            public void bindView(ViewHolder viewHolder, final BDeviceEntity bDeviceEntity, int i) {
                ((TextView) viewHolder.getView(R.id.name)).setText(bDeviceEntity.getDName());
                ((TextView) viewHolder.getView(R.id.type)).setText(bDeviceEntity.getDLabel());
                ((TextView) viewHolder.getView(R.id.parameter)).setText(bDeviceEntity.getDParameter());
                viewHolder.getView(R.id.layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("device", bDeviceEntity);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    }
                });
            }
        });
        rightAdapter.setData(deviceEntities);
        rightAdapter.setEmptyDataView(R.layout.view_empty);
        if (labels.size() > 0) {
            label = labels.get(0);
            setLeftList();
        }
    }

    void setLeftList() {
        BPointConfigEntity l = session.getBPointConfigEntityDao().queryBuilder()
                .where(BPointConfigEntityDao.Properties.PCLive.eq(label), BPointConfigEntityDao.Properties.PCProcess.eq("1")).unique();
        if (l != null && !TextUtils.isEmpty(l.getPCLabel()))
            leftDatas.addAll(Arrays.asList(l.getPCLabel().split(",")));
        leftAdapter.notifyDataSetChanged();
        if (leftDatas.size() > 0) {
            label2 = leftDatas.get(0);
            setRightList();
        } else rightAdapter.notifyDataSetChanged();
    }

    void setRightList() {
        search = searchInfo.getText().toString();
        QueryBuilder<BDeviceEntity> builder = session.getBDeviceEntityDao().queryBuilder();
        List<BDeviceEntity> entities = builder.where(BDeviceEntityDao.Properties.DMidtype.eq(label), BDeviceEntityDao.Properties.DLabel.eq(label2)
                , builder.or(BDeviceEntityDao.Properties.DName.like("%" + search + "%"), BDeviceEntityDao.Properties.DBParameter.like("%" + search + "%"))).list();
        deviceEntities.clear();
        deviceEntities.addAll(entities);
        rightAdapter.notifyDataSetChanged();
    }

    @OnClick(R.id.search_button)
    public void onViewClicked() {
        setRightList();
    }
}
